/**
 * @file STL_Mysql.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2020-09-30
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#ifndef _STL_MYSQL_H_
#define _STL_MYSQL_H_

#include <string>
#include <functional>
#include <thread>
#include <mutex>
#include <mysql/mysql.h>

namespace STL
{
    class STL_Mysql
    {
    public:
        typedef enum Status_e
        {
            None,
            Init,
            Connecting,
            Connected,
            Disconnected,
            Over
        } Status;
        typedef struct Option_t
        {
            std::string host;
            uint16_t prot;
            std::string username;
            std::string password;
            std::string database;
            Option_t()
            {
                host = "";
                prot = 3306;
                username = "";
                password = "";
                database = "";
            };
            Option_t(std::string host, uint16_t port, std::string username, std::string password, std::string database)
            {
                this->host = host;
                this->prot = prot;
                this->username = username;
                this->password = password;
                this->database = database;
            };
        } Option;
        typedef std::function<void(STL_Mysql::Status)> StatusCallback;
        typedef std::function<std::string()> QueryCallback;

    public:
        STL_Mysql(STL_Mysql::Option option);
        ~STL_Mysql();
        int start();
        int stop();
        void setStatusCallback(STL_Mysql::StatusCallback callback) { m_statusCallback = callback; };
        void setQueryCallback(STL_Mysql::QueryCallback callback) { m_QueryCallback = callback; };

    private:
        void setStatus(STL_Mysql::Status status);

    private:
        STL_Mysql::QueryCallback m_QueryCallback;
        STL_Mysql::StatusCallback m_statusCallback;
        STL_Mysql::Status m_status;
        STL_Mysql::Option m_option;
        MYSQL *m_connection;

    private:
        bool m_run;
        std::thread m_thread;
        std::mutex m_mutex;
        void mysqlThread();
    };
} // namespace STL
#endif // _STL_MYSQL_H_